package condominios.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


import condominios.excepcion.DAOExcepcion;
import condominios.excepcion.LoginExcepcion;
import condominios.modelo.Persona;
import condominios.util.ConexionBD;

public class PersonaDAO extends BaseDAO {
	
	public Collection<Persona> buscarPorNombreDNI(String nombreDNI)
			throws DAOExcepcion {
		String query = "select idPersona,nombre,apellidoPaterno,apellidoMaterno,idPerfil,email,numDocumento from persona where nombre like ? or numDocumento like ? or apellidoPaterno like ? or apellidoMaterno like ?";
		Collection<Persona> c = new ArrayList<Persona>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombreDNI + "%");
			stmt.setString(2, "%" + nombreDNI + "%");
			stmt.setString(3, "%" + nombreDNI + "%");
			stmt.setString(4, "%" + nombreDNI + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Persona vo = new Persona();
				
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setIdPerfil(rs.getInt("idPerfil"));
				vo.setNombres(rs.getString("nombre"));
				vo.setApellidoPaterno(rs.getString("apellidoPaterno"));
				vo.setApellidoMaterno(rs.getString("apellidoMaterno"));
				vo.setEmail(rs.getString("email"));
				vo.setDni (rs.getString("numDocumento"));
				
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public Persona insertar(Persona vo) throws DAOExcepcion {
		String query = "INSERT INTO persona (nombre,apellidoPaterno,apellidoMaterno,numDocumento,email,idPerfil, contrasenia,estado,estadoRegistro) VALUES (?,?,?,?,?,?,?,?,?)";
				
		System.err.println(query);
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getApellidoPaterno());
			stmt.setString(3, vo.getApellidoMaterno());
			stmt.setString(4, vo.getDni());
			stmt.setString(5, vo.getEmail());
			stmt.setInt(6, vo.getIdPerfil());
			stmt.setString(7, vo.getContrasenia());
			stmt.setInt(8,1);
			stmt.setInt(9,1);

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Persona> obtenerTodos()
			throws DAOExcepcion {
		String query = "select idPersona,nombre,apellidoPaterno,apellidoMaterno,idPerfil,email,numDocumento from persona";
		Collection<Persona> c = new ArrayList<Persona>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);	
			rs = stmt.executeQuery();
			while (rs.next()) {
				Persona vo = new Persona();
				
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setIdPerfil(rs.getInt("idPerfil"));
				vo.setNombres(rs.getString("nombre"));
				vo.setApellidoPaterno(rs.getString("apellidoPaterno"));
				vo.setApellidoMaterno(rs.getString("apellidoMaterno"));
				vo.setEmail(rs.getString("email"));
				vo.setDni (rs.getString("numDocumento"));
				
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public void actualizar(Persona vo) throws DAOExcepcion {
		System.out.println("PersonaDAO: actualizar(Persona vo)");
		String query = "update persona set nombre=?, apellidoPaterno=?, apellidoMaterno=?, idPerfil=?, email=?, numDocumento=? where idPersona=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getApellidoPaterno());
			stmt.setString(3, vo.getApellidoMaterno());
			stmt.setInt(4, vo.getIdPerfil());
			stmt.setString(5, vo.getEmail());
			stmt.setString(6, vo.getDni());
			stmt.setInt(7, vo.getIdPersona());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	public void eliminar(int numPersona) throws DAOExcepcion {
		System.out.println("PersonaDAO: eliminar(String personaNombre)");
		String query = "DELETE FROM persona WHERE idPersona=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, numPersona);
			int i = stmt.executeUpdate();
			if (i != 1) {

				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	public Persona validar(String numDoc, String clave)
			throws DAOExcepcion, LoginExcepcion {
		String query = "select idPersona, nombre, apellidoPaterno,"
				+ "apellidoMaterno, email, numDocumento, idPerfil, contrasenia "
				+ "from persona where numDocumento=? and contrasenia=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Persona vo = new Persona();
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, numDoc);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setNombres(rs.getString("nombre"));
				vo.setApellidoPaterno(rs.getString("apellidoPaterno"));
				vo.setApellidoMaterno(rs.getString("apellidoMaterno"));
				vo.setEmail(rs.getString("email"));
				vo.setDni(rs.getString("numDocumento"));
				vo.setIdPerfil(rs.getInt("idPerfil"));
				vo.setContrasenia(rs.getString("contrasenia"));
			} else {
				throw new LoginExcepcion("No existe");
			}
		} catch (LoginExcepcion e) {
			System.err.println(e.getMessage());
			throw new LoginExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Persona obtenerPornumDoc(String numDoc) throws DAOExcepcion {
		String query = "select idPersona, nombre, apellidoPaterno," 
				+ "apellidoMaterno, email, numDocumento, idPerfil, contrasenia "
				+ "from persona where numDocumento=?";
			
		Persona vo = new Persona();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, numDoc);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setNombres(rs.getString("nombre"));
				vo.setApellidoPaterno(rs.getString("apellidoPaterno"));
				vo.setApellidoMaterno(rs.getString("apellidoMaterno"));
				vo.setEmail(rs.getString("email"));
				vo.setDni(rs.getString("numDocumento"));
				vo.setIdPerfil(rs.getInt("idPerfil"));
				vo.setContrasenia(rs.getString("contrasenia"));
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Persona validarUsuario(String numDocumento, String clave)
			throws DAOExcepcion, LoginExcepcion {
		String query = "SELECT idPersona, idPerfil, numDocumento, nombre, apellidoPaterno, apellidoMaterno, fechaNacimiento, email, contrasenia, estado, estadoRegistro"
				+ " from persona where numDocumento=? and contrasenia=? and estadoRegistro=1 and estado=1";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Persona vo = new Persona();
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, numDocumento);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setIdPerfil(rs.getInt("idPerfil"));
				vo.setDni(rs.getString("numDocumento"));
				vo.setNombres(rs.getString("nombre"));
				vo.setApellidoPaterno(rs.getString("apellidoPaterno"));
				vo.setApellidoMaterno(rs.getString("apellidoMaterno"));
				vo.setEmail(rs.getString("email"));
			} else {
				throw new LoginExcepcion("No existe");
			}
		} catch (LoginExcepcion e) {
			System.err.println(e.getMessage());
			throw new LoginExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

}
